#include "../include/jcIluminacao.h"
#include <stdio.h>

jcIluminacao::jcIluminacao()
{

};

jcIluminacao::~jcIluminacao()
{

};

void jcIluminacao::jcVerificaLuz(IplImage* img, int x, int y, int intensidade)
{
	int R, G, B;
    R = ((uchar*)(img->imageData + img->widthStep*y))[x*3+2];
    G = ((uchar*)(img->imageData + img->widthStep*y))[x*3+1];
    B = ((uchar*)(img->imageData + img->widthStep*y))[x*3+0];

	intensidade = R;
	if(G>R) intensidade = G;
	if(B>G && B>R) intensidade = B;
}

void jcIluminacao::jcIncorporaLuz(IplImage* backg, IplImage* foreg, int x, int y)
{
	int LuzDoBackg;
	jcVerificaLuz(backg, x, y, LuzDoBackg);
	

	int R = ((uchar*)(foreg->imageData + foreg->widthStep*y))[x*3+2];
	int G = ((uchar*)(foreg->imageData + foreg->widthStep*y))[x*3+1];
	int B = ((uchar*)(foreg->imageData + foreg->widthStep*y))[x*3+0];

	if((R>=G)&&(R>=B))
	{
		((uchar*)(foreg->imageData + foreg->widthStep*y))[x*3+2] = LuzDoBackg;
		((uchar*)(foreg->imageData + foreg->widthStep*y))[x*3+1] = ((G/R) * (LuzDoBackg-R)) + G;
		((uchar*)(foreg->imageData + foreg->widthStep*y))[x*3+0] = ((B/R) * (LuzDoBackg-R)) + B;
	}

	if((G>=R)&&(G>=B))
	{
		((uchar*)(foreg->imageData + foreg->widthStep*y))[x*3+1] = LuzDoBackg;
		((uchar*)(foreg->imageData + foreg->widthStep*y))[x*3+2] = ((R/G) * (LuzDoBackg-G)) + R;
		((uchar*)(foreg->imageData + foreg->widthStep*y))[x*3+0] = ((B/G) * (LuzDoBackg-G)) + B;
	}
		
	if((B>=G)&&(B>=R))
	{
		((uchar*)(foreg->imageData + foreg->widthStep*y))[x*3+0] = LuzDoBackg;
		((uchar*)(foreg->imageData + foreg->widthStep*y))[x*3+1] = ((G/B) * (LuzDoBackg-B)) + G;
		((uchar*)(foreg->imageData + foreg->widthStep*y))[x*3+2] = ((R/B) * (LuzDoBackg-B)) + R;
	}
}
